package com.xr.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xr.exception.CustomException;
import com.xr.mapper.MenuMapper;
import com.xr.pojo.CheckGroup;
import com.xr.pojo.Menu;
import com.xr.pojo.MenuExample;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.service.MemuService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.sound.midi.Soundbank;
import java.util.List;

@Service(interfaceClass = MemuService.class)
public class MenuServiceImpl implements MemuService {

    @Autowired
    private MenuMapper menuMapper;

    //根据当前用户查询菜单
    @Override
    public List<Menu> queryMenuByUser(String userName) {
        return menuMapper.queryMenuByUser(userName);
    }

    //查询一级菜单
    @Override
    public List<Menu> queryByLevel1() {
        MenuExample me = new MenuExample();
        MenuExample.Criteria mec = me.createCriteria();
        mec.andLevelEqualTo(1);
        return menuMapper.selectByExample(me);
    }

    //查询所有+条件查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
//        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), true);
        Page<Menu> list = menuMapper.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(list.getTotal(), list.getResult());
    }

    //查询除所有父级菜单
    @Override
    public List<Menu> queryAllParentList() {
        MenuExample me = new MenuExample();
        MenuExample.Criteria mec = me.createCriteria();
        mec.andLevelEqualTo(1);
        return menuMapper.selectByExample(me);
    }


    //添加
    @Override
    public void add(Menu menu) {
        menuMapper.insertSelective(menu);
    }


    //根据id查询
    @Override
    public Menu queryById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    //修改菜单
    @Override
    public void update(Menu menu) {
        menuMapper.updateByPrimaryKeySelective(menu);
    }

    //根据父id查询出菜单名
    @Override
    public Menu findByParentId(Integer id) {
        return menuMapper.findByParentId(id);
    }

    //删除
    @Override
    public void delete(Integer id) throws CustomException {
        //判断是否有子类
        MenuExample me = new MenuExample();
        MenuExample.Criteria mec = me.createCriteria();
        mec.andParentmenuidEqualTo(id);
        List<Menu> menuList = menuMapper.selectByExample(me);
        System.out.println("menuList:" + menuList);
        if (menuList != null && menuList.size() > 0) {
            //有子类 则不能删除
            throw new CustomException("该菜单有子菜单 暂不能删除");
        } else {
            menuMapper.deleteByPrimaryKey(id);
        }

    }
}
